<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mutasi_mahasiswa extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_mutasi_mahasiswa');
		$this->load->model('mdl_fakultas');
		$this->load->model('mdl_prodi');
	}
	
	function index(){
		$this->load->view('master/mutasi_mahasiswa');
	}
	
	//master mahasiswa pindahan
	function master(){
		$this->load->view('master/master_mahasiswa_pindahan');
	}
	
	
	function grid(){
		$data = $this->mdl_mutasi_mahasiswa->getdata();
		echo $this->mdl_mutasi_mahasiswa->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_master(){
		$data = $this->mdl_mutasi_mahasiswa->getdata_master();
		echo $this->mdl_mutasi_mahasiswa->togrid($data['row_data'], $data['row_count']);
	}
	
	function detail($kode){
		$r = $this->mdl_mutasi_mahasiswa->getdatadetail($kode);
		$data['kd_mahasiswa'] = $kode;
		
		$data['thn_angkatan'] = $r->row()->thn_angkatan;
		$data['no_pendaftaran'] = $r->row()->no_pendaftaran;
		$data['nim'] = $r->row()->nim;
		$data['nama'] = $r->row()->nama;
		$data['alamat'] = $r->row()->alamat;
		$data['no_telp'] = $r->row()->no_telp;
		$data['nama_prodi'] = $r->row()->nama_prodi;
		$data['nama_fakultas'] = $r->row()->nama_fakultas;
		//$data['kd_dosen'] = '';
		$data['nama_ortu'] = $r->row()->nama_ortu;
		$data['alamat_ortu'] = $r->row()->alamat_ortu;
		$data['pekerjaan'] = $r->row()->pekerjaan;
		$data['telp_ortu'] = $r->row()->telp_ortu;
		$data['asal_universitas'] = $r->row()->asal_universitas;
		$data['telp_asal_universitas'] = $r->row()->telp_asal_universitas;
		$data['lampiran'] = $r->row()->lampiran;
		//$data['verifikasi'] = '';
		
		$this->load->view('master/mahasiswa_pindahan_detail', $data);
	}
	
	function add(){
		$data['kd_mahasiswa'] = '';
		$data['thn_angkatan'] = '';
		$data['no_pendaftaran'] = '';
		//$data['nim'] = '';
		$data['nama'] = '';
		$data['tempat_lahir'] = '';
		$data['tgl_lahir'] = date('d/m/Y');
		$data['alamat'] = '';
		$data['no_telp'] = '';
		$data['kd_prodi'] = '';
		//$data['kd_dosen'] = '';
		$data['nama_ortu'] = '';
		$data['alamat_ortu'] = '';
		$data['pekerjaan'] = '';
		$data['telp_ortu'] = '';
		$data['asal_universitas'] = '';
		$data['telp_asal_universitas'] = '';
		$data['file_upload'] = '';
		//$data['status'] = 'A';
		$this->load->view('mutasi_mhs/mutasi_mahasiswa_form', $data);
	}
	
	function save(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		$data['file_upload'] = '';
		
		# upload file
			if (!empty($_FILES['userfile']['name'])){ // jika ada file yg akan diupload
				$config['upload_path'] = './file_upload/Mutasi/';
				$config['allowed_types'] = 'pdf';
				$config['overwrite'] = TRUE;
				$config['max_size']	= '20000';
				$this->load->library('upload', $config);
				
				if (!$this->upload->do_upload()){
					// $error = array('error' => $this->upload->display_errors());
					// $this->load->view('form_upload', $error);
				}else{
					$up = $this->upload->data();
					$data['file_upload'] = $up['file_name'];
				}
			}
		
		# rules validasi form
		$this->form_validation->set_rules("thn_angkatan", 'Tahun Angkatan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("no_pendaftaran", 'Nomor Pendaftaran', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("nim", 'NIM', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama", 'Nama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat", 'alamat', 'trim|required|xss_clean');
		$this->form_validation->set_rules("no_telp", 'Telepon Siswa', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_prodi", 'Program Studi', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("kd_dosen", 'Dosen Wali', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama_ortu", 'Nama Orang Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat_ortu", 'Alamat Orang Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("pekerjaan", 'Pekerjaan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("telp_ortu", 'Telepon Oreng Tua', 'trim|required|xss_clean');
		$this->form_validation->set_rules("asal_universitas", 'Universitas Asal', 'trim|required|xss_clean');
		$this->form_validation->set_rules("telp_asal_universitas", 'Telepon Universitas Asal', 'trim|required|xss_clean');
		//$this->form_validation->set_rules("status", 'Status', 'trim|required|xss_clean');		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			$result = $this->mdl_mutasi_mahasiswa->InsertOnDb($data);
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	}
	
	function edit($kode){
		$r = $this->mdl_mutasi_mahasiswa->getdataedit($kode);
		$data['kd_mahasiswa'] = $kode;
		
		$data['thn_angkatan'] = $r->row()->thn_angkatan;
		$data['no_pendaftaran'] = $r->row()->no_pendaftaran;
		$data['nim'] = $r->row()->nim;
		$data['nama'] = $r->row()->nama;
		$data['tempat_lahir'] = $r->row()->tempat_lahir;
		$data['tgl_lahir'] = FormatDateFromMysql($r->row()->tgl_lahir);
		$data['alamat'] = $r->row()->alamat;
		$data['no_telp'] = $r->row()->no_telp;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		//$data['kd_dosen'] = '';
		$data['nama_ortu'] = $r->row()->nama_ortu;
		$data['alamat_ortu'] = '';
		$data['pekerjaan'] = '';
		$data['telp_ortu'] = '';
		$data['asal_universitas'] = $r->row()->asal_universitas;
		$data['telp_asal_universitas'] = $r->row()->telp_asal_universitas;
		$data['lampiran'] = $r->row()->lampiran;
		//$data['verifikasi'] = '';
		
		$this->load->view('mutasi_mhs/mutasi_mahasiswa_form_v', $data);
	}

	function verifikasi(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		$data['pesan_error'] = '';
		
		$result=$this->mdl_mutasi_mahasiswa->InsertOnDBverifikasi($data);
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}	
	
	function laporan_pdf($kd_prodi, $thn_angkatan){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['thn_angkatan'] = $thn_angkatan;
		
		 //$data['nama'] = '';
		 $data['namaUniv'] = 'STMIK BANDUNG';
		 $data['alamatUniv'] = 'Jl.Phh.Mustofa No. 39. Grand Surapati Core (SUCORE) Blok M No.19, Telp.022 - 7207777';
		 $data['kotaUniv'] = 'Bandung, Jawa Barat';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_mutasi_mahasiswa->get_pdf($fil);
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('master/mutasi_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('mutasimhs.pdf');
	}
	
}